package com.tec.security.utils.security.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.tec.security.utils.date.DateString;
import com.tec.security.utils.other.LimitPage;
import com.tec.security.utils.security.entity.UserInformation;
import com.tec.security.utils.security.entity.UserKey;
import com.tec.security.utils.security.entity.Userlog;
import com.tec.security.utils.security.mapper.UserKeyMapper;
import com.tec.security.utils.security.service.UserInformationService;
import com.tec.security.utils.security.service.UserlogService;
import com.tec.security.utils.security.mapper.UserlogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
* @author LL318
* @description 针对表【userlog】的数据库操作Service实现
* @createDate 2023-11-30 09:26:12
*/
@Service
public class UserlogServiceImpl extends ServiceImpl<UserlogMapper, Userlog> implements UserlogService{

    private final UserInformationService userInformationService;
    private final UserKeyMapper userKeyMapper;
    private final UserlogMapper userlogMapper;
    @Autowired
    public UserlogServiceImpl(UserInformationService userInformationService, UserKeyMapper userKeyMapper, UserlogMapper userlogMapper) {
        this.userInformationService = userInformationService;
        this.userKeyMapper = userKeyMapper;
        this.userlogMapper = userlogMapper;
    }

    /**
     * @author 陆林
     * @date 2023-11-30
     * @description 插入账号日志
     */
    @Override
    public void userInfo(String loginID,String message,String type) {
        UserInformation userInformation=userInformationService.selectInformationByUUID(loginID);
        QueryWrapper<UserKey> userKeyQueryWrapper=new QueryWrapper<>();
        userKeyQueryWrapper.select("username").eq("uuid",loginID);
        Userlog userlog=new Userlog();
        userlog.setName(userInformation.getName());
        userlog.setMessage(message);
        userlog.setUsername(userKeyMapper.selectOne(userKeyQueryWrapper).getUsername());
        userlog.setDate(DateString.currentDate());
        userlog.setTime(DateString.currentTime());
        userlog.setType(type);
        userlogMapper.insert(userlog);
    }

    /**
     * @author 陆林
     * @date 2023-11-30
     * @description 返回条件分页查询列表
     */
    @Override
    public Map<String,Object> allUserLogList(LimitPage limitPage, String type) {
        QueryWrapper<Userlog> userlogQueryWrapper=new QueryWrapper<>();
        PageHelper.startPage(limitPage.getPage(), limitPage.getSize());
        Map<String,Object> result=new HashMap<>();
        QueryWrapper<Userlog> userlogCountQueryWrapper=new QueryWrapper<>();
        if(!Objects.equals(type, "全部")){
            userlogQueryWrapper.eq("message",type);
            userlogCountQueryWrapper.eq("message",type);
        }
        result.put("list",userlogMapper.selectList(userlogQueryWrapper));
        result.put("total",userlogMapper.selectCount(userlogCountQueryWrapper));
        return result;
    }
}




